package com.beerandjesus.sportofkings.handicapper.profile.resolver;

import com.beerandjesus.sportofkings.handicapper.profile.AnalysisProfile;
import com.beerandjesus.sportofkings.handicapper.model.Medication;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.HorseRace;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;

import static com.beerandjesus.sportofkings.handicapper.profile.AnalysisProfileBuilder.analysisProfile;
import static com.beerandjesus.sportofkings.handicapper.profile.AnalysisProfile.Type.EARLY_PACE_TRYING_BLINKERS;
import static com.beerandjesus.sportofkings.handicapper.profile.AnalysisProfile.Type.MAIDEN_EQUIPMENT_CHANGE;
import static com.beerandjesus.sportofkings.handicapper.model.EquipmentChange.BLINKERS_ON;

/**
 * This profile identifies the following scenarios:
 *
 * - Maiden
 * - 2nd, 3rd or 4th time out
 * - First time using Lasix or Blinkers
 * - Additional point for high pace figure (early pace)
 *
 * OR
 *
 * - Non-maiden
 * - High pace figure (early pace)
 * - Switching to blinkers
 *
 **/
public class EquipmentChangeProfileResolver implements AnalysisProfileResolver {

    public static final int MINIMUM_QUIRIN_POINTS_FOR_EARLY_PACE = 6;

    public AnalysisProfile resolve(HorseRace race, Horse horse) {
        if (race.getRace().getType().isMaiden()) {
            // Must be 2nd,3rd, or 4th time out
            switch (horse.getNumberOfStarts()) {
                case 1:
                case 2:
                case 3:
                    break;
                default:
                    return null;
            }
            // Must be first time with Lasix or with blinkers
            if (isFirstTimeLasix(horse) ||
                    isFirstTimeWithBlinkers(horse)) {
                int score = 2;
                // Add a point for early pace horse with blinkers
                if (!isFirstTimeLasix(horse) && isEarlyPace(horse)) {
                    score = 3;
                }
                return analysisProfile(MAIDEN_EQUIPMENT_CHANGE, score);
            }
        } else {
            // Must be an early pace horse
            if (!isEarlyPace(horse)) {
                return null;
            }
            if (horse.getEquipmentChange().equals(BLINKERS_ON)) {
                return analysisProfile(EARLY_PACE_TRYING_BLINKERS, 0);
            }
        }
        return null;
    }

    private boolean isFirstTimeLasix(Horse horse) {
        return horse.getMedication() != null && horse.getMedication().equals(Medication.LAS_FIRST);
    }

    private boolean isEarlyPace(Horse horse) {
        String qPts = horse.getQuirinSpeedPoints();
        try {
            return Integer.valueOf(qPts) >= MINIMUM_QUIRIN_POINTS_FOR_EARLY_PACE;
        } catch (Exception e) {
            return false;
        }
    }

    private boolean isFirstTimeWithBlinkers(Horse horse) {
        for (PastPerformance pp : horse.getPastPerformances()) {
            if (pp.isWoreBlinkers()) {
                return false;
            }
        }
        return horse.getEquipmentChange().equals(BLINKERS_ON);
    }
}
